package game.worlds;

import hx.events.Keyboard;

/**
 * 记录一个玩家可用的按键键码
 */
class Key {
	/**
	 * 左键
	 */
	public var LEFT = Keyboard.A;

	/**
	 * 右键
	 */
	public var RIGHT = Keyboard.D;

	/**
	 * 上键
	 */
	public var UP = Keyboard.W;

	/**
	 * 下键
	 */
	public var DOWN = Keyboard.S;

	/**
	 * 攻击键
	 */
	public var ATTACK = Keyboard.J;

	/**
	 * 跳跃键 K
	 */
	public var JUMP = Keyboard.K;

	/**
	 * 快速移动键 L
	 */
	public var FAST_MOVE = Keyboard.L;

	/**
	 * 技能系列 U
	 */
	public var SKILL_U = Keyboard.U;

	/**
	 * 技能系列 I
	 */
	public var SKILL_I = Keyboard.I;

	/**
	 * 技能系列 O 一般为必杀技
	 */
	public var SKILL_O = Keyboard.O;

	/**
	 * 技能系列 P 一般为辅助技能
	 */
	public var SKILL_P = Keyboard.P;

	/**
	 * 当前接收的角色
	 */
	public var role:GameRole;

	public function new() {
		this.onInit();
	}

	public function onInit():Void {}

	public function dispose():Void {}

	/**
	 * 是否存在这个按键的支持
	 * @param key 
	 * @return Bool
	 */
	public function hasKeySupport(key:Int):Bool {
		return key == LEFT || key == RIGHT || key == UP || key == DOWN || key == ATTACK || key == SKILL_I || key == SKILL_O || key == SKILL_P
			|| key == JUMP || key == FAST_MOVE || key == SKILL_U;
	}

	/**
	 * 是否是攻击按键
	 * @param key 
	 * @return Bool
	 */
	public function hasAttackKey(key:Int):Bool {
		return key == ATTACK || key == SKILL_I || key == SKILL_O || key == SKILL_P || key == SKILL_U || key == FAST_MOVE;
	}

	public function onKeyDown(key:Int):Void {
		if (hasKeySupport(key)) {
			this.role.onKeyDown(key);
		}
	}

	public function onKeyUp(key:Int):Void {
		if (hasKeySupport(key)) {
			this.role.onKeyUp(key);
		}
	}

	/**
	 * 获得原始按键值，即ASDW JKLUIOP
	 * @param key 
	 * @return Int
	 */
	public function getOrginKey(key:Int):Int {
		if (key == UP) {
			return Keyboard.W;
		} else if (key == DOWN) {
			return Keyboard.S;
		} else if (key == LEFT) {
			return Keyboard.A;
		} else if (key == RIGHT) {
			return Keyboard.D;
		} else if (key == ATTACK) {
			return Keyboard.J;
		} else if (key == JUMP) {
			return Keyboard.K;
		} else if (key == FAST_MOVE) {
			return Keyboard.L;
		} else if (key == SKILL_I) {
			return Keyboard.I;
		} else if (key == SKILL_U) {
			return Keyboard.U;
		} else if (key == SKILL_O) {
			return Keyboard.O;
		} else if (key == SKILL_P) {
			return Keyboard.P;
		}
		return -1;
	}

	public function getFromKey(key:Int):Int {
		if (key == Keyboard.A) {
			return LEFT;
		} else if (key == Keyboard.D) {
			return RIGHT;
		} else if (key == Keyboard.W) {
			return UP;
		} else if (key == Keyboard.S) {
			return DOWN;
		} else if (key == Keyboard.J) {
			return ATTACK;
		} else if (key == Keyboard.K) {
			return JUMP;
		} else if (key == Keyboard.L) {
			return FAST_MOVE;
		} else if (key == Keyboard.I) {
			return SKILL_I;
		} else if (key == Keyboard.U) {
			return SKILL_U;
		} else if (key == Keyboard.O) {
			return SKILL_O;
		} else if (key == Keyboard.P) {
			return SKILL_P;
		}
		return -1;
	}
}
